# -*- coding=utf-8 -*-
'''
Created on 2012-4-22

@author: Administrator
'''
import wx,os,dbHelper
ID_OPEN = 100
ID_SAVE = 101
ID_EXIT = 102
ID_ABOUT=200
ID_ADD_CLASS = 301
ID_ADD_PAPER = 302
class KFrame(wx.Frame):
    '''
    this frame is used for 
    '''
    db = None
    classesdis={}#item to classid
    classesdis2={}#classid to item
    
    def __init__(self,parent,id,title):
        '''
        Constructor
        '''
        wx.Frame.__init__(self,parent,id,\
                          title,size=(800,600))
        self.tree = wx.TreeCtrl(self, -1, \
                                style=wx.TR_HAS_BUTTONS| wx.TR_LINES_AT_ROOT | wx.TR_DEFAULT_STYLE | wx.SUNKEN_BORDER)
        
        self.__set_tree()
        self.text = wx.TextCtrl(self,-1,\
                               style=wx.TE_MULTILINE)
        self.__set_menubar()
        self.__set_popup_menu()
        self.__set_layout()
        
    
    def __set_tree(self):
        self.db=dbHelper.DB()
        classes=dbHelper.DB.get_classes(self.db)
        datas=dbHelper.DB.get_datas(self.db)
        #print classes
        
        root = self.tree.AddRoot("分类")
        for i in xrange(len(classes)):
            treeitem = self.tree.AppendItem(root,classes[i][2])
            self.classesdis[treeitem]=classes[i][0]
            self.classesdis2[classes[i][0]]=treeitem
            
        for i in xrange(len(datas)):
            treeitem = self.tree.AppendItem(self.classesdis2[datas[i][1]],datas[i][2])
        
                
        self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.OnTreeItemRightClick, self.tree)
        self.tree.Expand(root)
        
    def OnTreeItemRightClick(self,event):
        #root
        if self.tree.GetRootItem()==event.GetItem():
            self.PopupMenu(self.popupMenuRoot)
            return
        #class
        if self.tree.GetItemParent(event.GetItem())==self.tree.GetRootItem():
            self.PopupMenu(self.popupMenuClass)

#TODO:add new class

    def __set_popup_menu(self):
        self.popupMenuRoot = wx.Menu()
        self.popupMenuRoot.Append(ID_ADD_CLASS,"add new class","add new class")
        wx.EVT_MENU(self,ID_ADD_CLASS,self.AddNewClass)
        self.popupMenuClass = wx.Menu()
        self.popupMenuClass.Append(ID_ADD_PAPER,"add new paper","add new paper")
        wx.EVT_MENU(self,ID_ADD_PAPER,self.AddNewPaper)
    
    def AddNewPaper(self,event):
        dlg=wx.TextEntryDialog(None,"please input a paper name","title","new paper name")
        if dlg.ShowModal()==wx.ID_OK:
            new_paper_name = dlg.GetValue()
        item = event.GetItem()
        classes = self.db.classes
        for i in xrange(len(classes)):
            if classes[i][3] == item:
                self.db.append_data(classes[i][1], new_paper_name, self.text.GetValue())
                break
        
        
        
        
                
        
            
#TODO:to be done 
    
    def AddNewClass(self,event):
        dlg= wx.TextEntryDialog(None,"please input a class name","title","new class name")
        if dlg.ShowModal()==wx.ID_OK:
            new_class_name=dlg.GetValue()
            classItem=self.tree.AppendItem(self.tree.GetRootItem(),new_class_name)
            classId=dbHelper.DB.append_class(self.db, new_class_name)
            self.classesdis[classItem]=classId
            self.classesdis2[classId]=classItem
        
                      
    def __set_menubar(self):
        self.CreateStatusBar()
        menuBar = wx.MenuBar()
        
        menufile = wx.Menu()
        menuhelp = wx.Menu()
        
        menufile.Append(ID_OPEN,"&open",\
                        "open a file")
        menufile.Append(ID_SAVE,"&save",\
                        "save the file")
        
        menufile.AppendSeparator()
        menufile.Append(ID_EXIT,'E&xit',\
                        'terminate')
        menuhelp.Append(ID_ABOUT,'A&bout',\
                        'info about')
        
        menuBar.Append(menufile,'&File')
        menuBar.Append(menuhelp,'&Help')
        
        self.SetMenuBar(menuBar)
        
        wx.EVT_MENU(self,ID_OPEN,self.OnOpen)
        wx.EVT_MENU(self,ID_SAVE,self.OnSave)
        wx.EVT_MENU(self,ID_EXIT,self.OnExit)
        wx.EVT_MENU(self,ID_ABOUT,self.OnAbout)
        
        
    def __set_properties(self):
        pass
    
    def __set_layout(self):
        sizer= wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.tree,1,wx.EXPAND,0)
        sizer.Add(self.text,2,wx.EXPAND,0)
        self.SetSizer(sizer)
        self.Layout()
        
    def OnOpen(self,event):
        dlg = wx.FileDialog(self,\
                            message='choose a file',\
                            defaultDir='',\
                            defaultFile='',\
                            wildcard='*.*',\
                            style=wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname,\
                                  self.filename),'r')
            
            self.text.SetValue(f.read())
            f.close()
        dlg.Destroy()
    def OnSave(self,event):
        itcontains = self.text.GetValue()
        f = open(os.path.join(self.dirname,\
                              self.filename),'w')
        f.write(itcontains)
        f.close()
    def OnExit(self,event):
        self.Close(True)
    def OnAbout(self,event):
        dlg = wx.MessageDialog(self,message ='A simple'' editor create by esf\n'\
                               'author:esf\n'
                               'date : 4 22',\
                               caption='About this program',\
                               style= wx.OK)
        dlg.ShowModal()
        dlg.Destroy()
        
if __name__=='__main__':
    app = wx.PySimpleApp()
    frame = KFrame(None,wx.ID_ANY,'Simple Editor')
    frame.Show()
    app.MainLoop()
        

    
    
        
        